/*
Exercise 4.2
 Temperature data logger
 http://tronixstuff.wordpress.com
 CC by-sa 3.0
 */
#include <LiquidCrystal.h> // we need this library for the LCD commands
LiquidCrystal lcd(10,11,12,13,14,15,16);
void setup() 
{
  Serial.begin(9600); // to send data to PC if connected
  lcd.begin(16, 2); // tells Arduino the LCD dimensions
  lcd.setCursor(0,0);
  lcd.print("  tronixstuff  "); // print text and move cursor to start of next line
  lcd.setCursor(0,1);
  lcd.print("* exercise 4.1 *");
  delay(3000);
  lcd.clear(); // clear LCD screen
  lcd.setCursor(0,0);
  lcd.print("  Press button  ");
  lcd.setCursor(0,1);
  lcd.print("    to start    ");
  pinMode(8,INPUT); 
}
int period = 1000; //period between samples in milliseconds - 1 hour = 3600000
float tempdata[] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // our array for the temperature data - 24 floating point variables
float temperature = 0; // used for temperature calculations
float mintemp = 0;
float maxtemp = 0;
float sumtemp = 0;
float avetemp = 0;

void loop()
{
  waitforpress(8);
  delay(200);
  for (int i=0; i<=23; i++)
  {
    temperature = analogRead(5);          // read the TMP36 temp sensor on analogue pin 5
    temperature = (temperature*5000)/1024; // convert raw sensor value to millivolts
    temperature = temperature-500;        // remove voltage offset
    temperature = temperature/10;         // convert millivolts to Celsius
    tempdata[i] = temperature;           // store the temperature for this sample in the array
    lcd.clear(); // clear LCD screen to tell people what is happening
    lcd.setCursor(0,0);
    lcd.print("Progress: ");
    lcd.print(i+1);
    lcd.print("/24 ");
    lcd.setCursor(0,1);
    lcd.print("Last: ");
    lcd.print(temperature,2);
    lcd.print(" deg C");
    delay(1000);       ////////////////////////////////// change this back to period
  }
  // finished loggin temperatures. Now to display the data to the user
  lcd.clear(); // clear LCD screen
  lcd.setCursor(0,0);
  lcd.print("Finished - Press");
  lcd.setCursor(0,1);
  lcd.print("to scroll data");
  waitforpress(8);
  delay(100); // saves having button debounce circuitry
  // analyse and compute min/max/average results
  mintemp = tempdata[0];
  maxtemp = tempdata[0];

  for (int a=0; a<=23; a++)
  {
    if (tempdata[a]>maxtemp)
    {
      maxtemp = tempdata[a];
    }
    if (tempdata[a]<mintemp)
    {
      mintemp = tempdata[a];
    }
  }
  // now to calculate the average temperature from our sample
  for (int z=0; z<=23; z++)
  {
    sumtemp = sumtemp + tempdata[z];
  }
  avetemp = sumtemp/24;
  for (int j = 0;  j<9999; j++) // basically an endless loop, reset arduino to start again
  {  
    // send data to serial so user can copy from serial monitor window to other application
    Serial.write(" * Sample data * ");
    Serial.println();
    for (int k = 0; k<=23; k++) // display sample data from array
    {
      Serial.print("Sample: ");
      Serial.print(k+1);
      Serial.print("/24 -  ");
      Serial.print(tempdata[k],2);
      Serial.print(" degrees Celsius");
      Serial.println();
    }
    Serial.write("Minimum temperature: ");
    Serial.print(mintemp,2);
    Serial.print(" degrees Celsius");
    Serial.println();
    Serial.write("Maximum temperature: ");
    Serial.print(maxtemp,2);
    Serial.print(" degrees Celsius");
    Serial.println();
    Serial.write("Average temperature: ");
    Serial.print(avetemp,2);
    Serial.print(" degrees Celsius");
    Serial.println();
    for (int k = 0; k<=23; k++) // display sample data from array
    {
      lcd.clear(); // clear LCD screen to tell people what is happening
      lcd.setCursor(0,0);
      lcd.print("Sample: ");
      lcd.print(k+1);
      lcd.print("/24  ");
      lcd.setCursor(0,1);
      lcd.print(tempdata[k],2);
      lcd.print(" degrees C");
      waitforpress(8);
      delay(200); // removes chance of switch bounce
    }
    waitforpress(8);
    delay(500); // removes chance of switch bounce
    lcd.clear(); // clear LCD screen to tell people what is happening
    lcd.setCursor(0,0);
    lcd.print(" Minimum temp.  ");
    lcd.setCursor(0,1);
    lcd.print(mintemp,2);
    lcd.print(" degrees C ");
    waitforpress(8);
    delay(200); // removes chance of switch bounce
    lcd.clear(); // clear LCD screen to tell people what is happening
    lcd.setCursor(0,0);
    lcd.print(" Maximum temp.  ");
    lcd.setCursor(0,1);
    lcd.print(maxtemp,2);
    lcd.print(" degrees C ");
    waitforpress(8);
    delay(200); // removes chance of switch bounce
    lcd.clear(); // clear LCD screen to tell people what is happening
    lcd.setCursor(0,0);
    lcd.print(" Average temp.  ");
    lcd.setCursor(0,1);
    lcd.print(avetemp,2);
    lcd.print(" degrees C ");
    waitforpress(8);
    delay(200); // removes chance of switch bounce
  }
}

void waitforpress(int inputpin)
// function to stop and wait for user to press button on input 
// pin number 'inputpin'. Ensure this is set to input using
// pinMode(input, pin number)
{
  while (digitalRead(inputpin)==0)
  {
    delay(10); // 
  }
}







